import request from '@/utils/request'

// 查询文章列表
export function getArticleList(pageNum: Number, pageSize: Number) {
  return request({
    url: '/article/articleList',
    method: 'get',
    headers: {
      isToken: false
    },
    params: {
      pageNum,
      pageSize
    }
  })
}

//查询最热文章
export function hotArticleList() {
  return request({
    url: '/article/hotArticleList',
    headers: {
      isToken: false
    },
    method: 'get'
  }).then((res: any) => {
    // 过滤内容
    res.data = res.data.map((item: any) => {
      item.content = item.content
        .replace(/<[^>]*>?/gm, '')
        .replace(/!\[.*?\]\(.*?\)/g, '')
        .replace(/#/g, '')
        .replace(/\*/g, '')
      // 提取前 50 个字符
      item.content = item.content.substring(0, 50) + '...'
      return item
    })
    return res
  })
}

//获取文章详情
export function getArticle(articleId: string) {
  return request({
    url: '/article/' + articleId,
    headers: {
      isToken: false
    },
    method: 'get'
  })
}

export function updateViewCount(articleId: string) {
  return request({
    url: '/article/updateViewCount/' + articleId,
    headers: {
      isToken: false
    },
    method: 'put'
  })
}

// 查询不同类型下的文章列表
export function whereArticleList(type: Number, typeId: String) {
  return request({
    url: `/article/where/list/${typeId}`,
    method: 'get',
    params: {
      type
    }
  })
}

// 时间轴
export const getTimeLine = () => {
  return request({
    url: '/article/timeLine',
    method: 'get'
  })
}
